package com.qqt.csr.common.sensitiveword;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * @Author:
 * @Date:
 * @Description: 敏感词显示规则
 **/
@Slf4j
@Getter
@AllArgsConstructor
public enum SensitiveWordShowTypeEnum {
    ORIGINAL_DISPLAYED(-1, "不做任何处理", (chars, startIndex, endIndex) -> {
        log.warn("未定义显示规则，不做任何处理");
        int len = endIndex - startIndex;
        return new String(chars, startIndex, len);
    }),

    ASTERISKS_DISPLAYED(0, "星号加密", (chars, startIndex, endIndex) -> {
        int wordLen = endIndex - startIndex;
        return "*".repeat(Math.max(0, wordLen));
    }),

    HIGHLIGHTED_DISPLAYED(1, "敏感词高亮", (chars, startIndex, endIndex) -> {
        int len = endIndex - startIndex;
        String sensitiveWord = new String(chars, startIndex, len);
        return String.format("<span style=\"color:red\">%s</span>", sensitiveWord);
    }),

    ;

    private final Integer code;

    private final String desc;

    private final ISensitiveWordDisplay sensitiveWordDisplay;


    public static SensitiveWordShowTypeEnum match(Integer code) {
        return Arrays.stream(SensitiveWordShowTypeEnum.values())
                .filter(item -> item.getCode().equals(code))
                .findFirst()
                .orElse(SensitiveWordShowTypeEnum.ORIGINAL_DISPLAYED);
    }

}
